package com.moji.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.moji.common.entity.MyPage;
import com.moji.common.utils.PageUtil;
import com.moji.entity.MjNews;
import com.moji.entity.MjOpinion;
import com.moji.entity.MjOrder;
import com.moji.entity.dto.QueryNewsDto;
import com.moji.mapper.MjNewsMapper;
import com.moji.service.MjNewsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moji.util.IdGenerator;
import com.moji.util.MoJiCommonUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 新闻中心表 服务实现类
 * </p>
 *
 * @author ccl
 * @since 2024-07-28
 */
@Service
public class MjNewsServiceImpl extends ServiceImpl<MjNewsMapper, MjNews> implements MjNewsService {

    @Resource
    private IdGenerator idGenerator;

    @Override
    public MyPage<MjNews> queryByPage(QueryNewsDto dto) {
        LambdaQueryWrapper<MjNews> wrapper = new LambdaQueryWrapper<>();
        //创建时间倒叙
        wrapper.orderByAsc(MjNews::getSort);
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<MjNews> mjNewsList = list(wrapper);
        PageInfo<MjNews> info = new PageInfo<>(mjNewsList);
        return PageUtil.PageInfo2MyPage(info);
    }

    @Override
    public boolean delete(Long id) {
        if (ObjectUtil.isEmpty(id)) {
            throw new RuntimeException("主键ID不能为空,请核查!");
        }
        return this.removeById(id);
    }

    @Override
    public boolean add(MjNews mjNews) {
        if (ObjectUtil.isEmpty(mjNews.getSort())) {
            throw new RuntimeException("新闻排序不能为空,请核查!");
        }
        String code = idGenerator.idGenerator("XWZX");
        mjNews.setCode(code);
        mjNews.setCreateId(MoJiCommonUtils.getUserCode());
        mjNews.setUpdateId(MoJiCommonUtils.getUserCode());
        mjNews.setCreateTime(new Date());
        mjNews.setUpdateTime(new Date());
        return this.save(mjNews);
    }

    @Override
    public boolean edit(MjNews mjNews) {
        MjNews mjNews1 = this.getById(mjNews.getId());
        BeanUtils.copyProperties(mjNews, mjNews1);
        mjNews1.setUpdateId(MoJiCommonUtils.getUserCode());
        mjNews1.setUpdateTime(new Date());
        return this.updateById(mjNews1);
    }

    @Override
    public MjNews detail(Long id) {
        if (ObjectUtil.isEmpty(id)) {
            throw new RuntimeException("主键ID不能为空,请核查!");
        }
        MjNews mjNews = this.getById(id);

        //上一篇
        LambdaQueryWrapper<MjNews> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.lt(MjNews::getSort, mjNews.getSort());
        wrapper1.orderByAsc(MjNews::getSort);
        wrapper1.last("limit 1");
        MjNews upMjNews = this.getOne(wrapper1);
        if (upMjNews != null) {
            mjNews.setUpNewsId(upMjNews.getId());
            mjNews.setUpNewsName(upMjNews.getTitle());
        }

        //下一篇
        LambdaQueryWrapper<MjNews> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.gt(MjNews::getSort, mjNews.getSort());
        wrapper2.orderByAsc(MjNews::getSort);
        wrapper2.last("limit 1");
        MjNews nextMjNews = this.getOne(wrapper2);
        if (nextMjNews != null) {
            mjNews.setNextNewsId(nextMjNews.getId());
            mjNews.setNextNewsName(nextMjNews.getTitle());
        }
        return mjNews;
    }
}
